# 编写一个函数ping，传递一个主机名，测试主机的连通性
# ping 3次
# 如果ping通，打印‘xxxx:up'，否则'xxxx:down'
# 并发扫描10个主机
import subprocess
import threading
def ping(host):
    result = subprocess.run(f'ping -c 3 {host} &>/dev/null', shell=True)
    # print(result)
    if result.returncode == 0:
        print(f'{host}: up')
    else:
        print(f'{host}: down')

if __name__ == '__main__':
    # ping('www.baidu.com')
    hosts = ['www.baidu.com', '192.168.88.100', 'www.google.com', 'www.qq.com', 'www.taobao.com', '127.0.0.1', 'www.jd.com', 'www.iqiyi.com', 'www.sina.com.cn', 'www.facebook.com']
    # for item in hosts:
    #     ping(item)
    for item in hosts:
        # 创建一个线程对象
        t = threading.Thread(target=ping, args=(item, ))
        t.start()
